﻿using EvertonApp.Transporte;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace EvertonApp.Data.UnitOfWork
{
    public class ComerciosUoW : IDisposable
    {

        private DataContext _dataContext;

        private IRepository<Comercio> _comerciosRepository;
        private IRepository<Oferta> _ofertasRepository;
        



        //Para usar mientras desarrollamos
        public ComerciosUoW()
        {
        }

        public ComerciosUoW(DataContext db)
        {
            this._dataContext = db;
        }

        public IRepository<Comercio> ComerciosRepository
        {
            get
            {
                if (this._comerciosRepository == null)
                {
                    //Mientras Desarrollamos
                    this._comerciosRepository = new TestComerciosRepository();
                }
                return _comerciosRepository;
            } 
        }


        public IRepository<Oferta> OfertasRepository { 
            get
            {
                if (this._ofertasRepository == null)
                {
                    //Mientras Desarrollamos
                    this._ofertasRepository = new TestOfertasRepository();
                }
                return _ofertasRepository;
            }
        }


        public bool Commit()
        {

            //Uso mientras desarrollo
            return true;

            //Para LINQ
            /*if (this._dataContext != null)
            {
                try
                {
                     _dataContext.SubmitChanges();
                    return true;
                }catch(ChangeConflictException e)
                {
                    Console.WriteLine(e.Message);
                    return false;
                 }
             }else
            {
                return false
            }*/

        }

        public bool Rollback()
        {
            //Uso mientras desarrollo
            return true;

            //Para LINQ
            /*if (this._dataContext != null)
            {
                this._dataContext = new DataContext(_dataContext.Connection.ConnectionString);
                return true;
            }else
            {
                return false;
            }*/
        }

        #region Implementation of IDisposable

        private bool _disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    this._dataContext.Dispose();
                }
            }
            this._disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
